using System;
using System.Collections.Generic;
using Clandestine.Engines.Field;
using Qyoto;

namespace Shlick.LayerModel
{
	internal class LayerItem : Item
	{
		public Layer Layer { get; private set; }
		
		private LayerItem(Layer item, MapDocument mapDocument, LayerTreeMode mode) : base(item, mapDocument, mode)
		{
			this.Layer = item;
		}
		
		public static LayerItem Get(Layer item, MapDocument mapDocument, LayerTreeMode mode)
		{
			if (cachedItems.ContainsKey(mode) && cachedItems[mode].ContainsKey(item))
				return (LayerItem)cachedItems[mode][item];
			else
				return new LayerItem(item, mapDocument, mode);
		} 
		
		public override void ContextMenuRequested (LayerTreeView parent, QPoint pos)
		{
			IContextMenu menu = new LayerMenu(parent, this.MapDocument, this.Layer);
			menu.Show();
		}
		
		public override int Row()
		{
			List<Layer> list = this.Layer.ContainingGroup.Layers;
			for (int i = 0; i < list.Count; i++)
				if (list[i] == this.Layer)
					return i;
			throw new Exception();
		}
		
		public override Item Child(int row)
		{
			return null; // end-node
		}
		
		public override Item Parent()
		{
			if (this.Layer.ContainingGroup == null)
				return null;
			else
				return LayerGroupItem.Get(this.Layer.ContainingGroup, this.MapDocument, this.Mode);
		}
		
		public override int ChildrenCount()
		{
			return 0; // end-node
		}
		
		public override string GetData()
		{
			 return String.Format("Layer");
		}
		
		public override int? GetDepth()
		{
			return this.Layer.Depth;
		}
		
		public override string GetName()
		{
			return this.Layer.Name;
		}
		
		public override bool SetName(string name)
		{
			this.Layer.Name = name;
			return true;
		}
		
		public override bool? IsVisible()
		{
			 return this.Layer.Visible;
		}
		
		public override bool SetVisible(bool visible)
		{
			this.Layer.Visible = visible;
			return true;
		}
		
		public override uint Flags ()
		{
			return base.Flags() | (uint)(ItemFlag.ItemIsSelectable | ItemFlag.ItemIsDragEnabled); //| ItemFlag.ItemIsDropEnabled);
		}
	}
}